package com.epoint.mobileoa.ui.personchoose;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.bizlogic.mobileoa.GetPersonChooseAdapterTask;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.LogHelp;
import com.epoint.mobileoa.tools.StringHelp;

/**
 * @author liyc
 * @time 2012-2-3 下午8:06:00
 * @annotation 人员选择
 */
public class PersonChooseViewNew extends SuperView {

	public static final int GetPersonChooseAdapter = 1;

	private ArrayList<String> backflaglist = new ArrayList<String>();// 用作返回按钮作标记
	private List<Map<String, Object>> mData;
	public static List<Map<String, String>> clist = new ArrayList<Map<String, String>>();// 选择的数据
	public static List<Map<String, String>> deptslist = new ArrayList<Map<String, String>>();// 选择的部门

	private ListView lv;
	private EditText et;// 人员选择显示框

	private Button btClean_pc;// 清空按钮
	private Button btConfirm_pc;// 确定按钮
	private Button btCX;// 查询按钮
	private EditText etCX;// 查询条件文本框
	private RelativeLayout pc1_ll_cz;// 查询框
	private String ouguid = "";// 跟部门

	@Override
	public void initControls() {
		top_left_bt.setOnClickListener(this);
		lv = (ListView) findViewById(R.id.lvContacts);
		et = (EditText) findViewById(R.id.et_pcselected);
		btClean_pc = (Button) findViewById(R.id.btClean_pc);
		btConfirm_pc = (Button) findViewById(R.id.btConfirm_pc);
		setTextViewFontColor(et);
		setTextViewFontColor(btClean_pc);
		setTextViewFontColor(btConfirm_pc);
		etCX = (EditText) findViewById(R.id.et_cz1);
		btCX = (Button) findViewById(R.id.bt_cz1);
		pc1_ll_cz = (RelativeLayout) findViewById(R.id.pc1_ll_cz);
		btCX.setOnClickListener(this);
		btClean_pc.setOnClickListener(this);
		btConfirm_pc.setOnClickListener(this);
		lv.setOnItemClickListener(this);

		// 设置根部门
//		ouguid = getIntent().getStringExtra("ouguid");
//		ouguid = ouguid == null ? "" : ouguid;

		// 是否隐藏查询栏目
		boolean hidesearch = getIntent().getBooleanExtra("hidesearch", false);

		// if(hidesearch){
		// pc1_ll_cz.setVisibility(View.GONE);
		// }
		// else{
		pc1_ll_cz.setVisibility(View.VISIBLE);
		// }

		mData = getData(ouguid);

		setTask();

		if (clist.size() > 0) {
			String[] info = StringHelp.dealChooseListToString(clist);
			et.setText(info[0]);
		}

	}

	void setTask() {
		top_pb.setVisibility(View.VISIBLE);
		HashMap param = new HashMap();
		param.put("mData", mData);
		param.put("clist", clist);
		param.put("deptslist", deptslist);
		param.put("et", et);
		Task task = new GetPersonChooseAdapterTask(this, param);
		MainService.addTask(task);
	}

	public static void initData() {
		clist.clear();
		deptslist.clear();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		try {
			if (mData.get(position).get("info").equals("部门")) {
				backflaglist.add(mData.get(position).get("guid").toString());
				mData = getData(mData.get(position).get("guid").toString());
				setTask();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onClick(View v) {
		if (v == top_left_bt) {
			backLogic();
		} else if (v == btClean_pc) {
			clist.clear();
			deptslist.clear();
			et.setText("");

			mData = getData(ouguid);

			setTask();
		} else if (v == btConfirm_pc) {
			finish();
		} else if (v == btCX) {
			mData = getDataForCX(etCX.getText().toString().trim());
			setTask();
			InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(etCX.getWindowToken(), 0);
		}
	}

	private List<Map<String, Object>> getData(String guid) {
		List<Map<String, Object>> list = DBHelp
				.getRootDeptMapReturnForPersonChoose(this, 0, guid);
		return list;
	}

	private List<Map<String, Object>> getDataForCX(String tj) {
		List<Map<String, Object>> list = DBHelp.getUserOnPyForPersonChoose(
				this, tj);
		return list;
	}

	public void backLogic() {
		/**
		 * 返回按钮操作逻辑 liyc 2011.08.02
		 */
		if (backflaglist.size() == 0) {
			finish();
		} else if (backflaglist.size() == 1) {
			mData = getData(ouguid);
			setTask();
		} else {
			mData = getData(backflaglist.get(backflaglist.size() - 2));
			setTask();
		}
		if (backflaglist.size() > 0) {
			backflaglist.remove(backflaglist.size() - 1);
		}
	}

	@Override
	public void refresh(Object... param) {
		if (param[0].toString().equals(MainService.NORMAL)) {
			lv.setAdapter((ListAdapter) param[1]);
		}
		top_pb.setVisibility(View.INVISIBLE);
	}

	@Override
	public int getLayoutInt() {
		return R.layout.personchoose1;
	}

	@Override
	public String setTitle() {
		return "人员选择";
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}

}
